﻿using System;
using System.Reflection;
using System.Web;

namespace FredCK.FCKeditorV2.ImageUpload
{
    /// <summary>
    /// 图片上传接口
    /// 接收GET参数 ImageUploadAssembly，根据参数值反射出自定义图片处理对象
    /// </summary>
    public class Uploader : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (Request.QueryString["ImageUploadAssembly"] != null)
            {
                string ass = Request.QueryString["ImageUploadAssembly"].Trim();
                ass = (ass == string.Empty ? "Heart,FCKEditorSimpleNet.Web.MyImageUploadHandler.Default" : ass);
                if (ass != string.Empty)
                {
                    string[] assArray = ass.Split(',');

                    if (assArray.Length == 2)
                    {
                        if (assArray[0].Trim() != string.Empty && assArray[1].Trim() != string.Empty)
                        {
                            //获取上传文件
                            HttpPostedFile hpf = Request.Files["NewFile"];
                            if (hpf != null)
                            {
                                FredCK.FCKeditorV2.ImageUpload.Base customUploader = null;
                                bool ok = true;

                                try
                                {
                                    customUploader = (FredCK.FCKeditorV2.ImageUpload.Base)this.createObject(assArray[0].Trim(), assArray[1].Trim());
                                    customUploader.PostFile = hpf;
                                }
                                catch
                                {
                                    ok = false;
                                    FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "自定义处理类不存在或设置的值不正确！");
                                }

                                if (ok)
                                {
                                    //调用自定义类中的Save方法
                                    customUploader.Save();                                    
                                }
                            }
                            else
                            {
                                FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "未获取上传文件！");
                            }
                        }
                        else
                        {
                            FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "参数有误！");
                        }
                    }
                    else
                    {
                        FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "参数有误！");
                    }
                }
                else
                {
                    FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "参数有误！");
                }
            }
            else
            {
                FredCK.FCKeditorV2.ImageUpload.Base.SendFileUploadResponse(false, "", "参数有误！");
            }
        }

        /// <summary>
        /// 反射创建对象
        /// </summary>
        /// <param name="assemblyName">程序集名称</param>
        /// <param name="classFullName">完整类名称</param>
        /// <returns></returns>
        private object createObject(string assemblyName, string classFullName)
        {
            return Assembly.Load(assemblyName).CreateInstance(classFullName);
        }
    }
}
